package controller;

import com.alibaba.fastjson.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import pojo.Comment;
import service.CommentService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;
    int i = 0;
    public CommentService getCommentService() {
        return commentService;
    }

    public void setCommentService(CommentService commentService) {
        this.commentService = commentService;
    }
    @ResponseBody
    @RequestMapping(value = "/getAllComment.do")
    public String getAllCommentComtroller(Comment comment){
        System.out.println("进入根据id 查询评论");
        List<Comment> allComment = commentService.getAllComment(comment);
        System.out.println(allComment.toString());
        //把查询出来的数据传给页面
        return JSONArray.toJSONString(allComment);
    }

    @ResponseBody
    @RequestMapping(value = "/getAllComments.do")
    public String getAllCommentComtrollers(){
        List<Comment> allComment = commentService.getAllComments();
        System.out.println(allComment.toString());
        //把查询出来的数据传给页面
        return JSONArray.toJSONString(allComment);
    }

    /*@RequestMapping(value = "/getAllComments.do",method = RequestMethod.GET)
    public String getAllCommentComtrollers(HttpServletRequest request){
        System.out.println("进入");
        List<Comment> allComment = commentService.getAllComments();
        //把查询出来的数据传给页面
        request.getSession().setAttribute("allComment",allComment);
        // 页面跳转
        return "list";
    }*/
    @ResponseBody
    @RequestMapping(value = "/add.do")
    public String addCommentComtroller(Comment comment, HttpSession session){
        System.out.println("进入评论");
        int i = commentService.addComment(comment);
        if(i > 0){
            System.out.println("添加成功");
            return JSONArray.toJSONString("评论成功");
        }else{
            System.out.println("添加失败");
            return JSONArray.toJSONString("评论失败");
        }
    }
    @RequestMapping(value = "/delete.do")
    public ModelAndView deleteCommentComtroller(@RequestParam(required = false, defaultValue = "0") Integer id){
        ModelAndView modelAndView = new ModelAndView();
        System.out.println("前端获取到的id="+id);
        int i = commentService.deleteComment(id);
        if(i > 0){
            System.out.println("删除成功");
            modelAndView.addObject("cg","删除成功");
        }else {
            System.out.println("删除失败");
            modelAndView.addObject("cg","删除失败");
        }
        return modelAndView;
    }
    @RequestMapping(value = "/update.do",method = RequestMethod.POST)
    public String updateCommentClickdComtroller(Comment comment,HttpServletRequest request) {
        i++;
        if(i%2!=0){
            request.getSession().setAttribute("shu",1);
        }else{
            request.getSession().setAttribute("shu",0);

        }
        System.out.println("进入点赞功能");
        int i = commentService.updateCommentClickd(comment);
        if (i > 0) {
            System.out.println("点赞成功");
            return "redirect:/comment/getAllComments.do";
        }
        return "redirect:/comment/getAllComments.do";
    }
    @ResponseBody
    @RequestMapping(value = "/zan.do")
    public String updateCommentClickdComtrollers(Comment comment) {
        System.out.println("点赞状态"+comment.getZt());
        System.out.println("进入点赞功能");
        int i = commentService.updateCommentClickd(comment);
        if (i > 0) {
            System.out.println("点赞成功");
            return JSONArray.toJSONString("点赞成功");
        }else {
            return JSONArray.toJSONString("点赞失败");
        }
    }
    @ResponseBody
    @RequestMapping(value = "/counts.do")
    public String selectzan(@RequestParam(required = false, defaultValue = "0") Integer id){
        System.out.println("进入查询评论条数 id为"+id);
        int i = commentService.selectan(id);
        System.out.println("id为"+id+"有"+i+"条评论");
        return JSONArray.toJSONString(i);
    }
}
